﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Mani.Common;

public partial class Controls_platts_index : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadPlatts(null,1, true,gvPlatts.SortExpression,gvPlatts.SortDirection.ToString());
    }

    private void LoadPlatts(string keyword,int pageNumber, bool setPageCount,string sortBy,string desc)
    {
        ViewState["CurrentPage"] = pageNumber;
        IPlattService plattService = new PlattService();
        if (setPageCount)
            gvPlatts.VirtualItemCount = plattService.CountSearchPlatts(keyword);

        gvPlatts.DataSource = plattService.SearchPlatts(keyword, gvPlatts.PageSize, pageNumber, sortBy, desc);
        gvPlatts.DataBind();

    }

    private void LoadPlatts(DateTime fromDate, DateTime toDate, int pageNumber, bool setPageCount, string sortBy, string desc)
    {
        ViewState["CurrentPage"] = pageNumber;
        IPlattService plattService = new PlattService();
        if (setPageCount)
            gvPlatts.VirtualItemCount = plattService.CountSearchDatePlatts(fromDate,toDate);

        gvPlatts.DataSource = plattService.SearchDatePlatts(fromDate,toDate, gvPlatts.PageSize, pageNumber, sortBy, desc);
        gvPlatts.DataBind();
    
    }

    protected void gvPlatts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPlatts.PageIndex = e.NewPageIndex;
        LoadPlatts(tbSearch.Text,e.NewPageIndex + 1, false,gvPlatts.SortExpression,gvPlatts.SortDirection.ToString());
    }

    protected void gvPlatts_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        IPlattService plattService = new PlattService();
        plattService.Delete(Convert.ToInt32(gvPlatts.DataKeys[e.RowIndex].Value));
        LoadPlatts(tbSearch.Text,1, true,gvPlatts.SortExpression,gvPlatts.SortDirection.ToString());
    }

    protected void gvPlatt_Sorting(object sender, GridViewSortEventArgs e)
    {
        
        
        if (ViewState["sort"] == null || ViewState["sort"].ToString() == "asc")
        {
            e.SortDirection = SortDirection.Descending;
            ViewState["sort"] = "desc";
        }
        else
        {
            e.SortDirection = SortDirection.Ascending;
            ViewState["sort"] = "asc";
        }

        LoadPlatts(tbSearch.Text, gvPlatts.PageIndex + 1, false, e.SortExpression, e.SortDirection.ToString());
    }

    protected void lbSearch_Click(object sender, EventArgs e)
    {
        LoadPlatts(tbSearch.Text,1,true,gvPlatts.SortExpression,gvPlatts.SortDirection.ToString());
    }

    protected void btSearchDate_Click(object sender, EventArgs e)
    {
        DateTime fromDate = Convert.ToDateTime(tbDateSearchFrom.Text);
        DateTime toDate = Convert.ToDateTime(tbDateSearchTo.Text);
        if (DateTime.Compare(fromDate, toDate) <= 0)
            LoadPlatts(fromDate, toDate, 1, true, gvPlatts.SortExpression, gvPlatts.SortDirection.ToString());
        else
        {
            UIHelper.ClientRedirect(this.Page, "?platts_index");
            UIHelper.ClientAlert(GetLocalResourceObject("SelectDateError").ToString());
        }
    }
}
